<template>
  <Layout>
    <div class="fade-in">
      <h2 class="text-2xl font-bold text-gray-800 mb-6">订单管理</h2>

      <div v-if="orders.length === 0" class="bg-white rounded-xl shadow-sm p-8 text-center text-gray-500">
        <i class="fas fa-clipboard-list text-4xl mb-4"></i>
        <p>您还没有任何订单</p>
        <router-link to="/products" class="text-indigo-600 hover:underline mt-4 inline-block">去购物</router-link>
      </div>

      <div v-else class="bg-white rounded-xl shadow-sm overflow-hidden table-container">
        <table class="min-w-full divide-y divide-gray-200">
          <thead class="bg-gray-50">
            <tr>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">订单号</th>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">下单时间</th>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">总金额</th>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">状态</th>
              <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase tracking-wider">操作</th>
            </tr>
          </thead>
          <tbody class="bg-white divide-y divide-gray-200">
            <tr v-for="order in orders" :key="order.id" class="hover:bg-gray-50">
              <td class="px-6 py-4 whitespace-nowrap">{{ order.id }}</td>
              <td class="px-6 py-4 whitespace-nowrap">{{ formatDateTime(order.order_date) }}</td>
              <td class="px-6 py-4 whitespace-nowrap">¥{{ order.total_amount.toFixed(2) }}</td>
              <td class="px-6 py-4 whitespace-nowrap">
                 <span
                  class="px-2 py-1 rounded-full text-sm"
                   :class="{
                    'bg-blue-100 text-blue-800': order.status === 'pending',
                    'bg-green-100 text-green-800': order.status === 'completed',
                    'bg-red-100 text-red-800': order.status === 'cancelled'
                  }"
                >
                  {{ getStatusName(order.status) }}
                 </span>
              </td>
              <td class="px-6 py-4 whitespace-nowrap space-x-2">
                <router-link :to="`/orders/${order.id}`" class="text-indigo-600 hover:text-indigo-900">
                  查看详情
                </router-link>
              </td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>
  </Layout>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import Layout from '../components/Layout.vue'
import { orderService } from '../services/api' // 导入订单服务
import moment from 'moment' // 导入moment库用于时间格式化

const orders = ref([])

// 格式化日期时间
const formatDateTime = (dateTime) => {
  return moment(dateTime).format('YYYY-MM-DD HH:mm')
}

// 获取订单状态名称
const getStatusName = (status) => {
  const statuses = {
    pending: '待处理',
    completed: '已完成',
    cancelled: '已取消'
  }
  return statuses[status] || status
}

// 加载订单列表
const loadOrders = async () => {
  try {
    console.log('Loading orders...'); // 添加日志
    const response = await orderService.getAll();
    console.log('Orders response:', response); // 添加日志
    
    if (response && response.data) {
      orders.value = response.data;
    } else {
      console.error('Unexpected response format:', response);
      orders.value = [];
    }
  } catch (error) {
    console.error('Failed to load orders:', error);
    if (error.response) {
      console.error('Error response:', error.response.data);
    }
    orders.value = [];
  }
};

onMounted(() => {
  loadOrders()
})
</script>

<style scoped>
.table-container {
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
  border-radius: 10px;
  overflow: hidden;
}

.table-header {
  background: linear-gradient(135deg, #f8fafc 0%, #edf2f7 100%);
}

.btn-primary {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  transition: all 0.3s;
}

.btn-primary:hover {
  transform: translateY(-2px);
  box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
}

/* 美化表格 */
table {
  border-collapse: collapse;
  width: 100%;
}

thead tr {
  border-bottom: 1px solid #e0e0e0;
}

th {
  background-color: #f5f5f5; /* 与主内容区背景色一致 */
  color: #616161; /* 深灰色文字 */
  text-align: left;
  padding: 12px 24px; /* 增加内边距 */
  font-weight: 600;
}

tbody tr {
  border-bottom: 1px solid #eeeeee;
}

tbody tr:last-child {
  border-bottom: none;
}

td {
  padding: 12px 24px; /* 增加内边距 */
  vertical-align: middle;
}

tbody tr.hover\:bg-gray-50:hover {
  background-color: #fafafa; /* 浅色悬停背景 */
}

/* 美化状态标签 */
.px-2.py-1.rounded-full.text-sm {
    padding: 4px 8px;
    border-radius: 9999px; /* 更圆的角 */
    font-weight: 600;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

.fade-in {
  animation: fadeIn 0.3s ease-out;
}
</style> 